﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ciloci.Flee;
using Ciloci.Flee.CalcEngine;
using SimpleSim.SimEngine;

namespace SimpleSim.WpfApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private CalculationEngine _engine;
        private ExpressionContext _context;
        public MainWindow()
        {
            InitializeComponent();

            _engine = new CalculationEngine();
            _context = new ExpressionContext();
            
            _context.Imports.AddType(typeof(AnalogTag));

            _context.Variables.Add("TAG1", new AnalogTag());
            _context.Variables.Add("TAG2", new AnalogTag());

            foreach (var variable in _context.Variables)
            {
                AnalogTag tag = (AnalogTag) variable.Value;
                //tag.Name = variable.Key;
                //tag.NameChanging += TagNameChanged;
            }
            
            dataGridTagDatabase.ItemsSource = _context.Variables.Values;
            dataGridTagDatabase.SelectionChanged += DataGridTagDatabaseSelectionChanged;
            
        }

        //private void TagNameChanged(object sender, AnalogTag.NameChangingEventArgs nameChangingEventArgs)
        //{
        //    AnalogTag tag = (AnalogTag)sender;
        //    try
        //    {
        //        _context.Variables.Add(nameChangingEventArgs.NewName, tag);
        //        _context.Variables.Remove(nameChangingEventArgs.OldName);
        //    }
        //    catch (Exception e)
        //    {
        //        nameChangingEventArgs.Cancel = true;
        //    }
        //    dataGridTagDatabase.ItemsSource = _context.Variables;
        //}

        void DataGridTagDatabaseSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(dataGridTagDatabase.SelectedItem != null)
            {
                gridTagDetails.DataContext = dataGridTagDatabase.SelectedItem;
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem) e.Source;

            if((string) menuItem.Header == "_Exit")
                Close();
            else if ((string)menuItem.Header == "_Add Tag")
            {
                try
                {
                    _context.Variables.Add("NewTag", new AnalogTag());
                    //((AnalogTag) _context.Variables["NewTag"]).Name = "NewTag";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                dataGridTagDatabase.ItemsSource = _context.Variables.Values;
            }
            else if ((string)menuItem.Header == "_Remove Tag")
            {
                Debug.Print("{0}",dataGridTagDatabase.SelectedIndex);
                if (dataGridTagDatabase.SelectedIndex != -1)
                {
                    _context.Variables.Remove(_context.Variables.Keys.ElementAt(dataGridTagDatabase.SelectedIndex));
                    dataGridTagDatabase.ItemsSource = _context.Variables.Values;
                }
            }
        }

        private void TagDetailsTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            
        }
    }
}
